/*
 * Copyright 2007-2010 kingbi.
 * Creator: Janins
 */
package com.kingbi.system.function.service;

import java.sql.SQLException;
import java.util.List;

import com.kingbi.system.function.dao.FunctionDao;
import com.kingbi.system.function.dao.FunctionQuery;
import com.kingbi.system.function.model.FunctionModel;


/**
 * @author mouzb
 * 
 */
public class FunctionServiceImpl implements FunctionService
{

    private FunctionDao functionDao;

    public void setFunctionDao(FunctionDao thisDao)
    {
        functionDao = thisDao;
    }

    public List<Object> loadOrgTree(String qeurySql, Object[] params) throws SQLException
    {
        try
        {
            return functionDao.findBySQL(qeurySql, params);
        }
        catch (SQLException e)
        {
            throw e;
        }
    }

    public void saveFunction(FunctionModel functionModel)
    {
        // 设置此功能可用状态
        if(!(functionModel.getFunStatus() != null && !functionModel.getFunStatus().equals("") && !functionModel
                .getFunStatus().equals("null")))
        {
            functionModel.setFunStatus("1");
        }

        // 设置此功能是否有子菜单
        if(!(functionModel.getHasChild() != null && !functionModel.getHasChild().equals("") && !functionModel
                .getHasChild().equals("null")))
        {
            functionModel.setHasChild("0");
        }

        /*
         * 根据是否有父菜单设置 菜单层级 及父菜单状态
         */
        if(functionModel.getFatherFunCode() != null && !functionModel.getFatherFunCode().equals("")
                && !functionModel.getFatherFunCode().equals("null"))// 有父菜单
        {
            if(!(functionModel.getFatherFunCode().equals("0")))
            {
                /*
                 * 根据父菜单的层级 设置本菜单层级
                 */
                FunctionModel searchModel = new FunctionModel();
                searchModel.setFuncCode(functionModel.getFatherFunCode());
                List<FunctionModel> funList = this.listFunction(searchModel, 0, 100);
                FunctionModel fatherFunModel = new FunctionModel();
                fatherFunModel = funList.get(0);
                // 设置本级菜单层级
                functionModel
                        .setFuncLevel(Integer.parseInt(fatherFunModel.getFuncLevel()) + 1 + "");

                // 设置所选择的父菜单hasChild为有子菜单标记
                if(fatherFunModel.getHasChild() != null && fatherFunModel.getHasChild().equals("0"))
                {
                    fatherFunModel.setHasChild("1");
                    functionDao.save(fatherFunModel);
                }
            }
            else
            {
                functionModel.setFatherFunCode("0");
                functionModel.setFuncLevel("1");
            }
        }
        else
        {
            functionModel.setFatherFunCode("0");
            functionModel.setFuncLevel("1");
        }
        functionDao.save(functionModel);
    }

    public FunctionModel loadFunction(int functionID)
    {
        return functionDao.findById(functionID);
    }

    public List<FunctionModel> loadFunctionByCode(String functionCode)
    {
        return functionDao.findCmd("loadFunctionByCode", new Object[] { functionCode }, -1, -1);
    }

    public void deleteFunction(int functionID)
    {
        functionDao.delete(functionDao.findById(functionID));
    }

    public List<FunctionModel> listFunction(FunctionModel functionModel, int start, int max)
    {
        FunctionQuery functionQuery = new FunctionQuery();
        functionQuery.doSearchQuery(functionModel);
        return functionDao.findByHQL(functionQuery.getSearch_query_bud().toString(), functionQuery
                .getParams(), start, max);
    }

    public List<FunctionModel> loadVerifyFunction(int page, int max, Object[] sql_params)
    {
        return functionDao.findCmd("loadVerifyPack", sql_params, page, max);
    }

    public long queryCount(FunctionModel functionModel)
    {
        FunctionQuery functionQuery = new FunctionQuery();
        functionQuery.doSearchQuery(functionModel);
        return functionDao.getCountByHQL(functionQuery.getSearch_count_bud().toString(),
                functionQuery.getParams());
    }

}
